package me.fung.rem.app.settings

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import me.fung.keen.app.view.Presenter
import me.fung.rem.bean.database
import me.fung.rem.bean.repo.*
import org.ktorm.dsl.delete
import org.ktorm.dsl.eq
import org.ktorm.entity.toList

class TagPresenter : Presenter() {
    var textNewTag by mutableStateOf("")
    var tags = mutableStateListOf<TagInfo>()

    fun load() {
        val a = database.tagInfoes.toList()
        tags.clear()
        tags.addAll(a)
    }

    fun add() {
        if (textNewTag.isBlank()) return
        val tag = TagInfo(0, textNewTag.trimIndent())
        tag.add()
        textNewTag = ""
        tags.add(tag)
    }

    fun drop(t: TagInfo) {
        database.delete(TagInfoes) {
            it.id eq t.id
        }
        tags.remove(t)
    }
}